﻿using System;
using MessageCommon.GlobalStructure;
using System.Reflection;
using System.Collections.Generic;

namespace MessageBase
{
    public class MessageObject : MarshalByRefObject
    {
        public Dictionary<string, AddressInfoList> m_dicUserIPList = new Dictionary<string, AddressInfoList>();

        public MessageObject()
        {
        }

        public void RegisterUser(string strName, AddressInfo address)
        {
            if (!m_dicUserIPList.ContainsKey(strName))
            {
                m_dicUserIPList.Add(strName, AddAddress(null, address));
            }
            else
            {
                m_dicUserIPList[strName] = AddAddress(m_dicUserIPList[strName], address);
            }
        }

        public void RegisterUser()
        {

        }

        public void UnRegisterUser(string strName, AddressInfo address)
        {
            if (m_dicUserIPList.ContainsKey(strName))
            {
                AddressInfoList lstAdress = m_dicUserIPList[strName];
                if (lstAdress != null)
                {
                    lstAdress.RemoveAll(addr =>
                        {
                            return address == addr;
                        });
                }
            }
        }

        public AddressInfoList GetUserOnlineAddress(string strName)
        {
            if (m_dicUserIPList.ContainsKey(strName))
            {
                return m_dicUserIPList[strName];
            }
            else
            {
                return new AddressInfoList();
            }
        }

        private AddressInfoList AddAddress(AddressInfoList lstAddress, AddressInfo address)
        {
            if (lstAddress == null)
            {
                lstAddress = new AddressInfoList();
            }

            lstAddress.Add(address);
            return lstAddress;
        }
    }
}
